home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / testdisk / src / rfs_dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-09  |  12.0 KB  |  503 lines

  1. /*
  2.  
  3.     File: rfs_dir.c
  4.  
  5.     Copyright (C) 1998-2004 Christophe GRENIER <grenier@cgsecurity.org>
  6.     Some code from Yury Umanets <torque@ukrpost.net>
  7.   
  8.     This software is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 2 of the License, or
  11.     (at your option) any later version.
  12.   
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.   
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.  */
  23. #if defined(HAVE_PROGSREISERFS30) || defined(HAVE_PROGSREISERFS31_OLD) | defined(HAVE_PROGSREISERFS31)
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <sys/stat.h>
  30. #include <time.h>
  31. #include "dal/dal.h"
  32. #ifdef HAVE_PROGSREISERFS30
  33. #elif defined(HAVE_PROGSREISERFS31_OLD)
  34. #include "dal/file_dal.h"
  35. #else
  36. #include "dal/file.h"
  37. #endif
  38. #include "reiserfs/reiserfs.h"
  39. #include "reiserfs/libprogs_tools.h"
  40. #include "types.h"
  41. #include "common.h"
  42. #include "intrface.h"
  43. #include "rfs.h"
  44. #include "dir.h"
  45.  
  46. struct list_dir_struct {
  47.     t_file_data *dir_list;
  48.     t_file_data *current_file;
  49.     reiserfs_fs_t *current_fs;
  50.     int flags;
  51. };
  52.  
  53. #ifdef HAVE_PROGSREISERFS30
  54. dev_t dal_dev(dal_t *dal) {
  55.     
  56.     if (!dal)
  57.     return 0;
  58.     return (dev_t)dal->dev;
  59. }
  60. #endif
  61.  
  62. #ifdef HAVE_PROGSREISERFS30
  63. size_t dal_block_size(dal_t *dal) {
  64.  
  65.     if (!dal) return 0;
  66.  
  67.     return dal->block_size;
  68. }
  69. #else
  70. unsigned dal_get_blocksize(dal_t *dal) {
  71.   if (!dal)
  72.     return 0;
  73.   return dal->blocksize;
  74. }
  75. #endif
  76.  
  77. /*
  78. size_t dal_blocksize(dal_t *dal) {
  79.  
  80.     if (!dal) return 0;
  81.  
  82. #ifdef HAVE_PROGSREISERFS30
  83.     return dal->block_size;
  84. #else
  85.     return dal->blocksize;
  86. #endif
  87. }
  88. */
  89.  
  90. char *dal_error(dal_t *dal) {
  91. #ifdef HAVE_PROGSREISERFS30
  92.     return "";
  93. #elif defined(HAVE_PROGSREISERFS31_OLD)
  94.     return "";
  95. #else
  96.     return dal->error;
  97. #endif
  98. }
  99.  
  100. static int file_read(dal_t *dal, void *buff, blk_t block, blk_t count) {
  101.   dword off, blocklen;
  102.   t_my_data *my_data=(t_my_data*)dal->data;
  103. /* ecrit_rapport("reiser file_read(dal=%p,buff=%p,block=%ld, count=%ld)\n",dal,buff,block,count); */
  104.   if (!dal || !buff)
  105.     return 0;
  106. #ifdef HAVE_PROGSREISERFS30
  107.   off = (dword)block * ((dword)dal->block_size/SECTOR_SIZE);
  108.   blocklen = (dword)count * ((dword)dal->block_size/SECTOR_SIZE);
  109. #else
  110.   off = (dword)block * ((dword)dal->blocksize/SECTOR_SIZE);
  111.   blocklen = (dword)count * ((dword)dal->blocksize/SECTOR_SIZE);
  112. #endif
  113. /* ecrit_rapport("blocklen=%ld\n",blocklen); */
  114.   if(my_data->disk_car->read(my_data->disk_car,blocklen,buff,my_data->partition->lba+off))
  115.     return 0;
  116.   return 1;
  117. }
  118.  
  119. static int file_write(dal_t *dal, void *buff, blk_t block, blk_t count)
  120. {
  121.   dword off, blocklen;
  122. /* ecrit_rapport("reiser file_write\n"); */
  123.   if (!dal || !buff)
  124.     return 0;
  125. #ifdef HAVE_PROGSREISERFS30
  126.   off = (dword)block * ((dword)dal->block_size/SECTOR_SIZE);
  127.   blocklen = (dword)count * ((dword)dal->block_size/SECTOR_SIZE);
  128. #else
  129.   off = (dword)block * ((dword)dal->blocksize/SECTOR_SIZE);
  130.   blocklen = (dword)count * ((dword)dal->blocksize/SECTOR_SIZE);
  131. #endif
  132. /* if(my_data->disk_car->write(my_data->disk_car,blocklen,buff,my_data->partition->lba+off)) */
  133.     return 0;
  134. /* return 1; */
  135. }
  136.  
  137. static int file_sync(dal_t *dal)
  138. {
  139.   if (!dal) return 0;
  140. /* ecrit_rapport("reiser file_sync\n"); */
  141.   return 1;
  142. }
  143.  
  144. static int file_flags(dal_t *dal)
  145. {
  146. /* ecrit_rapport("reiser file_flags\n"); */
  147.   if (!dal) return 0;
  148.   return dal->flags;
  149. }
  150.  
  151. static int file_equals(dal_t *dal1, dal_t *dal2)
  152. {
  153. /* ecrit_rapport("reiser file_equals\n"); */
  154.   if (!dal1 || !dal2)
  155.     return 0;
  156.   return !strcmp((char *)dal1->data, (char *)dal2->data);
  157. }
  158.  
  159. #ifdef HAVE_PROGSREISERFS30
  160. static int file_stat(dal_t *dal,struct stat *st)
  161. {
  162. /* ecrit_rapport("reiser file_stat\n"); */
  163.   if (!dal)
  164.     return 0;
  165. /* return (unsigned int)st.st_dev; */
  166.   return 1;
  167. }
  168. #else
  169. static unsigned int file_stat(dal_t *dal)
  170. {
  171. /* ecrit_rapport("reiser file_stat\n"); */
  172.   if (!dal)
  173.     return 0;
  174. /* return (unsigned int)st.st_dev; */
  175.   return 1;
  176. }
  177. #endif
  178.  
  179. #ifdef HAVE_PROGSREISERFS30
  180. static blk_t   file_len(dal_t *dal) {
  181. #elif defined(HAVE_PROGSREISERFS31_OLD)
  182. static blk_t   file_len(dal_t *dal) {
  183. #else
  184. static count_t file_len(dal_t *dal) {
  185. #endif
  186.   dword max_off = 0;
  187.   t_my_data *my_data=(t_my_data*)dal->data;
  188. /* ecrit_rapport("reiser file_len\n"); */
  189.   if (!dal) return 0;
  190.   max_off=my_data->partition->part_size*SECTOR_SIZE;
  191. #ifdef HAVE_PROGSREISERFS30
  192.   return max_off / dal->block_size;
  193. #else
  194.   return max_off / dal->blocksize;
  195. #endif
  196. }
  197.  
  198. static struct dal_ops ops = {
  199.     read: file_read, 
  200.     write: file_write, 
  201.     sync: file_sync, 
  202.     flags: file_flags, 
  203.     equals: file_equals, 
  204.     stat: file_stat, 
  205.     len: file_len,
  206. #ifdef HAVE_PROGSREISERFS30
  207.     dev: dal_dev
  208. #endif
  209. };
  210.  
  211. void dal_close(dal_t *dal) {
  212. /* ecrit_rapport("reiser dal_close\n"); */
  213.     
  214.     if (!dal) return;
  215.     
  216.     dal->ops = NULL;
  217.     dal->data = NULL;
  218.     FREE(dal);
  219. }
  220.  
  221. void file_close(dal_t *dal) {
  222. /* ecrit_rapport("reiser file_close\n"); */
  223.     if (!dal) return;
  224.     dal_close(dal);
  225. }
  226.  
  227. static int power_of_two(unsigned long value) {
  228.     return (value & -value) == value;
  229. }
  230.  
  231. #ifdef HAVE_PROGSREISERFS30
  232. dal_t *dal_open(struct dal_ops *myops, const void *dev, size_t blocksize, int flags, void *data)
  233. #elif defined(HAVE_PROGSREISERFS31_OLD)
  234. dal_t *dal_open(struct dal_ops *myops, const void *dev, size_t blocksize, int flags, void *data)
  235. #else
  236. dal_t *dal_open(struct dal_ops *myops, unsigned blocksize, int flags, void *data) 
  237. #endif
  238. {
  239.     dal_t *dal;
  240. /* ecrit_rapport("reiser dal_open\n"); */
  241.     
  242.     if (!myops) return NULL;
  243.     
  244.     if (!power_of_two(blocksize)) {
  245.         fprintf(stderr, "Block size isn't power of two.\n");
  246.         return NULL;
  247.     }    
  248.     
  249.     if (!(dal = (dal_t *)MALLOC(sizeof(*dal))))
  250.         return NULL;
  251.  
  252.     memset(dal, 0, sizeof(*dal));
  253.     
  254.     dal->ops = myops;
  255. #ifdef HAVE_PROGSREISERFS30
  256.     dal->block_size = blocksize;
  257. #elif defined(HAVE_PROGSREISERFS31_OLD)
  258.     dal->blocksize = blocksize;
  259. #else
  260.     dal->entity= NULL;
  261.     strncpy(dal->name, "/dev/reiserfs",sizeof(dal->name));
  262.     dal->error[0]=0;
  263.     dal->blocksize = blocksize;
  264. #endif
  265.     dal->data = data;
  266.     dal->flags = flags;
  267.     return dal;
  268. }
  269.  
  270. #ifdef HAVE_PROGSREISERFS30
  271. int dal_set_block_size(dal_t *dal, unsigned blocksize) {
  272.  
  273.     if (!dal) return 0;
  274.     
  275.     if (!power_of_two(blocksize))
  276.         return 0;
  277.     dal->block_size = blocksize;
  278.     return 1;
  279. }
  280. #else
  281. int dal_set_blocksize(dal_t *dal, unsigned blocksize) {
  282.  
  283.     if (!dal) return 0;
  284.     
  285.     if (!power_of_two(blocksize))
  286.         return 0;
  287.     dal->blocksize = blocksize;
  288.     return 1;
  289. }
  290. #endif
  291.  
  292.  
  293. int dal_read(dal_t *dal, void *buff, blk_t block, blk_t count) {
  294.  
  295. /* ecrit_rapport("reiser dal_read\n"); */
  296.     if (!dal) return 0;
  297.  
  298.     if (dal->ops->read)
  299.         return dal->ops->read(dal, buff, block, count);
  300.     
  301.     return 0;
  302. }
  303.  
  304. int dal_write(dal_t *dal, void *buff, blk_t block, blk_t count) {
  305.  
  306. /* ecrit_rapport("reiser dal_write\n"); */
  307.     if (!dal) return 0;
  308.     
  309.     if (dal->ops->write)
  310.         return dal->ops->write(dal, buff, block, count);
  311.         
  312.     return 0;
  313. }
  314.     
  315. int dal_sync(dal_t *dal) {
  316.  
  317.     if (!dal) return 0;
  318.  
  319.     if (dal->ops->sync)
  320.         return dal->ops->sync(dal);
  321.     
  322.     return 0;    
  323. }
  324.  
  325. int dal_flags(dal_t *dal) {
  326.  
  327.     if (!dal) return 0;
  328.  
  329.     if (dal->ops->flags)
  330.         return dal->ops->flags(dal);
  331.     
  332.     return 0;
  333. }
  334.  
  335. int dal_equals(dal_t *dal1, dal_t *dal2) {
  336. /* ecrit_rapport("reiserfs dal_equals\n"); */
  337.     
  338.     if (!dal1 || !dal2) return 0;
  339.  
  340.     if (dal1->ops->equals)
  341.         return dal1->ops->equals(dal1, dal2);
  342.     
  343.     return 0;    
  344. }
  345.  
  346. #if defined(HAVE_PROGSREISERFS31_OLD) || defined(HAVE_PROGSREISERFS30)
  347. int dal_stat(dal_t *dal, struct stat *mystat)
  348. {
  349. /* ecrit_rapport("reiserfs dal_stat\n"); */
  350.     if (!dal) return 0;
  351.     if (dal->ops->stat)
  352.         return dal->ops->stat(dal,mystat);
  353.     return 0;
  354. }
  355. #else
  356. unsigned int dal_stat(dal_t *dal) {
  357. /* ecrit_rapport("reiserfs dal_stat\n"); */
  358.     if (!dal) return 0;
  359.     if (dal->ops->stat)
  360.         return dal->ops->stat(dal);
  361.     return 0;
  362. }
  363. #endif
  364.  
  365. blk_t dal_len(dal_t *dal) {
  366. /* ecrit_rapport("reiserfs dal_len\n"); */
  367.     
  368.     if (!dal) 
  369.         return 0;
  370.  
  371.     if (dal->ops->len)
  372.         return dal->ops->len(dal);
  373.  
  374.     return 0;
  375. }
  376.  
  377. t_file_data *reiser_dir(t_param_disk *disk_car, const t_diskext *partition, t_dir_data *dir_data, const unsigned long int cluster);
  378. t_file_data *reiser_dir(t_param_disk *disk_car, const t_diskext *partition, t_dir_data *dir_data, const unsigned long int cluster)
  379. {
  380.   struct list_dir_struct *ls=(struct list_dir_struct*)dir_data->private_dir_data;
  381.   reiserfs_dir_t *dir;
  382.   reiserfs_dir_entry_t entry;
  383.   ls->dir_list=NULL;
  384.   ls->current_file=NULL;
  385.   if (!(dir = reiserfs_dir_open(ls->current_fs, dir_data->current_directory))) {
  386.     aff_buffer(BUFFER_ADD,"Couldn't open dir\n");
  387.     ecrit_rapport("Couldn't open dir\n");
  388.     return NULL;
  389.   }
  390.   while (reiserfs_dir_read(dir, &entry))
  391.   {
  392.     unsigned int thislen;
  393.     char name[MAX_NAME_LEN(DEFAULT_BLOCK_SIZE)];
  394.     reiserfs_object_t *entity;
  395.     strncpy(name,dir_data->current_directory,sizeof(name));
  396.     strcat(name,"/");
  397.     strcat(name,entry.de_name);
  398.     if((entity=reiserfs_object_create(ls->current_fs,name,1)))
  399.     {
  400.       t_file_data *new_file=MALLOC(sizeof(*new_file));
  401.       thislen=(MAX_NAME_LEN(DEFAULT_BLOCK_SIZE)<DIR_NAME_LEN?MAX_NAME_LEN(DEFAULT_BLOCK_SIZE):DIR_NAME_LEN);
  402.       memcpy(new_file->name,entry.de_name,thislen);
  403.       new_file->name[thislen]='\0';
  404.  
  405.       new_file->prev=ls->current_file;
  406.       new_file->next=NULL;
  407.       new_file->filestat.st_size=entity->stat.st_size;
  408.       memcpy(&new_file->filestat,&entity->stat,sizeof(new_file->filestat));
  409.       reiserfs_object_free(entity);
  410.       if(ls->current_file)
  411.     ls->current_file->next=new_file;
  412.       else
  413.     ls->dir_list=new_file;
  414.       ls->current_file=new_file;
  415.     }
  416.   }
  417.   reiserfs_dir_close(dir);
  418.   return ls->dir_list;
  419. }
  420.  
  421.  
  422. int dir_partition_reiser(WINDOW *window,t_param_disk *disk_car,t_diskext *partition, const int debug)
  423. {
  424.   dal_t *dal;
  425.   reiserfs_fs_t *fs;
  426.   reiserfs_path_node_t *leaf;
  427.   t_my_data my_data;
  428.   int32_t offset = 0;
  429.   int error = 0;
  430.  
  431.   aff_buffer(BUFFER_RESET,"Q");
  432.   wmove(window,4,0);
  433.   aff_part(window,AFF_PART_NL,disk_car,partition);
  434.   ecrit_rapport("\n");
  435.   aff_part_rapport(disk_car,partition);
  436.   my_data.partition=partition;
  437.   my_data.disk_car=disk_car;
  438. #ifdef HAVE_PROGSREISERFS30
  439.   dal=dal_open(&ops, "",DEFAULT_BLOCK_SIZE, O_RDONLY, &my_data);
  440. #elif defined(HAVE_PROGSREISERFS31_OLD)
  441.   dal=dal_open(&ops, "",DEFAULT_BLOCK_SIZE, O_RDONLY, &my_data);
  442. #else
  443.   dal=dal_open(&ops, DEFAULT_BLOCK_SIZE, O_RDONLY, &my_data);
  444. #endif
  445.   if (!dal)
  446.   {
  447.     ecrit_rapport("Couldn't open device\n");
  448.     aff_buffer(BUFFER_ADD,"Couldn't open device\n");
  449.     goto dir_partition_reiser_error;
  450.   }
  451. /* ecrit_rapport("file_open ok\n"); */
  452.  
  453. #ifdef HAVE_PROGSREISERFS30
  454.   if (!(fs = reiserfs_fs_open(dal, dal))) {
  455. #else
  456.   if (!(fs = reiserfs_fs_open_fast(dal, dal))) {
  457. #endif
  458.     ecrit_rapport("Couldn't open reiser filesystem %s\n",dal_error(dal));
  459.     aff_buffer(BUFFER_ADD,"Couldn't open reiser filesystem\n");
  460.     goto error_free_dal;
  461.   }
  462. /* ecrit_rapport("reiserfs_fs_open_fast ok\n"); */
  463.   {
  464.     struct list_dir_struct ls;
  465.     t_dir_data dir_data;
  466.     ls.dir_list=NULL;
  467.     ls.current_file=NULL;
  468.     ls.current_fs=fs;
  469.     ls.flags = 0; /*DIRENT_FLAG_INCLUDE_EMPTY; */
  470.     dir_data.window=window;
  471.     dir_data.debug=debug;
  472.     dir_data.private_dir_data=&ls;
  473.     dir_data.get_dir=reiser_dir;
  474.     strncpy(dir_data.current_directory,"/",sizeof(dir_data.current_directory));
  475.     dir_partition(disk_car,partition,&dir_data,2);
  476.   }
  477.   reiserfs_fs_close(fs);
  478.   file_close(dal);
  479.   return 0;
  480. error_free_dal:
  481.   file_close(dal);
  482. dir_partition_reiser_error:
  483.   aff_buffer(BUFFER_DISPLAY,"Q",window);
  484.   return 0xff;    
  485. }
  486.  
  487. #else
  488. #include "types.h"
  489. #include "common.h"
  490. #include "intrface.h"
  491.  
  492. int dir_partition_reiser(WINDOW *window,t_param_disk *disk_car,t_diskext *partition)
  493. {
  494.   aff_buffer(BUFFER_RESET,"Q");
  495.   wmove(stdscr,5,0);
  496.   aff_part(window,AFF_PART_NONL,disk_car,partition);
  497.   aff_part_rapport(disk_car,partition);
  498.   aff_buffer(BUFFER_ADD,"Recompile with progsreiserfs library");
  499.   aff_buffer(BUFFER_DISPLAY,"Q",window);
  500.   return 0;
  501. }
  502. #endif
  503.